﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
using Ledict.ConditionExtract.Common;
namespace Ledict.ConditionExtract.DAL
{
    public class CaseCauseWordDAL
    {
        private string DbName;
        public CaseCauseWordDAL()
        {
            DbName = "LdtCommonManage";
        }

        /// <summary>
        /// 获取关键词数据
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public DataSet GetCaseCauseWordData(string ccid)
        {
            DataSet ds = new DataSet();
            DataTable dtGjc = new DataTable();
            dtGjc.Columns.AddRange(new DataColumn[] { new DataColumn("gjcNum"), new DataColumn("gjc1"), new DataColumn("gjc2"), new DataColumn("gjc3"), });

            DataTable dtPcc = new DataTable();
            dtPcc.Columns.AddRange(new DataColumn[] { new DataColumn("PccNum"), new DataColumn("PccContent") });

            string sql = @"  select * from CaseCauseWord where cc_id= '" + ccid + "' ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                if (otB.Select(" CCW_Type=1 ").Length > 0)
                {
                    DataTable dt = otB.Select(" CCW_Type=1 ").CopyToDataTable();
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        IEnumerable<IGrouping<string, DataRow>> result = dt.Rows.Cast<DataRow>().GroupBy<DataRow, string>(dr => dr["CCW_GroupCode"].ToString());//C# 对DataTable中的某列分组，result中的Key是分组后的值
                        foreach (IGrouping<string, DataRow> item in result)
                        {

                            DataRow drs = dtGjc.NewRow();
                            List<DataRow> drrs = item.ToList();
                            if (drrs.Count == 1)
                            {
                                drs["gjc1"] = drrs[0]["CCW_KeyWord"].ToString();
                            }
                            else if (drrs.Count == 2)
                            {
                                drs["gjc1"] = drrs[0]["CCW_KeyWord"].ToString();
                                drs["gjc2"] = drrs[1]["CCW_KeyWord"].ToString();
                            }
                            else if (drrs.Count == 3)
                            {
                                drs["gjc1"] = drrs[0]["CCW_KeyWord"].ToString();
                                drs["gjc2"] = drrs[1]["CCW_KeyWord"].ToString();
                                drs["gjc3"] = drrs[2]["CCW_KeyWord"].ToString();
                            }
                            dtGjc.Rows.Add(drs);
                        }
                        DataView dv = dtGjc.DefaultView;
                        dv.Sort = " gjc1 desc ";
                        DataTable dt1= dv.ToTable();
                        for (int j = 0; j < dt1.Rows.Count; j++)
                        {
                            dt1.Rows[j]["gjcNum"] = j + 1;
                        }
                        ds.Tables.Add(dt1);
                    }
                   
                }

                if (otB.Select(" CCW_Type=2 ").Length > 0)
                {
                    DataTable dt = otB.Select(" CCW_Type=2 ").CopyToDataTable();
                    int i = 1;
                    foreach (DataRow dr in dt.Rows)
                    {
                        DataRow dr1 = dtPcc.NewRow();
                        dr1["PccNum"] = i;
                        dr1["PccContent"] = dr["CCW_KeyWord"].ToString();
                        dtPcc.Rows.Add(dr1);
                        i++;
                    }
                    ds.Tables.Add(dtPcc);
                }
            }
            return ds;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool AddCaseCauseWord(CaseCauseWord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"insert into CaseCauseWord(
			CCW_ID,CC_ID,CCW_GroupCode,CCW_KeyWord,CCW_Type)
			values (@CCW_ID,@CC_ID,@CCW_GroupCode,@CCW_KeyWord,@CCW_Type)");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@CCW_ID", DbType.String, model.CCW_ID);
            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, model.CC_ID);
            oBase.AddInParameter(oCmd, "@CCW_GroupCode", DbType.String, model.CCW_GroupCode);
            oBase.AddInParameter(oCmd, "@CCW_KeyWord", DbType.String, model.CCW_KeyWord);
            oBase.AddInParameter(oCmd, "@CCW_Type", DbType.Decimal, model.CCW_Type);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteCaseCauseWord(string CC_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CaseCauseWord ");
            strSql.Append(" where CC_ID=@CC_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }




    }
}
